//
//  MTNavigationController.m
//  ShiShiBang
//
//  Created by songbaoqiang on 16/9/1.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTNavigationController.h"

@interface MTNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation MTNavigationController
+ (void)load
{
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:17];
    attr[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [bar setTitleTextAttributes:attr];
     [bar setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0x45c75a)] forBarMetrics:UIBarMetricsDefault];
    
}

// <_UINavigationInteractiveTransition: 0x7ff7a4aa0300>:手势代理
- (void)viewDidLoad {
    [super viewDidLoad];
    
    id target = self.interactivePopGestureRecognizer.delegate;
    // 创建全屏滑动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    // 控制全屏手势什么时候触发
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
    
    // 限制系统边缘滑动手势
    self.interactivePopGestureRecognizer.enabled = NO;
    
}

#pragma mark - UIGestureRecognizerDelegate
// 判断下是否接收这个手指,触发手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 每次触发手势的时候就会调用
    // 判断下当前在不在根控制器
    return self.childViewControllers.count > 1;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 判断下当前控制器是否是非跟控制器
    if (self.childViewControllers.count > 0) { // 非根控制器
        // 非根控制器才需要设置返回按钮
        
        // 导航条上按钮位置 不能自己决定
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        [backButton setTitle:@"返回" forState:UIControlStateNormal];
        [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [backButton setImage:[UIImage imageNamed:@"back anxia"] forState:UIControlStateHighlighted];
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [backButton sizeToFit];
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        // 设置导航条左边按钮为返回按钮
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        
        // 隐藏底部条
        viewController.hidesBottomBarWhenPushed = YES;
        //        NSLog(@"%d",self.interactivePopGestureRecognizer.enabled);
    }
    
    
    // 真正执行跳转
    [super pushViewController:viewController animated:animated];
    
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}
@end
